import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_AppHomeEntity.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/page/news/news_detaile_page.dart';
import 'package:flutter_learn_vi/src/page/study/course/course_video_detail_page.dart';
import 'package:flutter_learn_vi/src/page/train/train_list_details_page.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/string_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 3/29/21.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class HomeItemBottomWidget extends StatefulWidget {
  final BeanAppHomeEntity entity;

  const HomeItemBottomWidget({Key key, this.entity}) : super(key: key);

  @override
  _HomeItemBottomWidgetState createState() => _HomeItemBottomWidgetState();
}

class _HomeItemBottomWidgetState extends State<HomeItemBottomWidget> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
        onTap: () {
          NavigatorUtils.pushPage(
            context,
            buildDetailPage(),
          );
        },
        child: buildContainer());
  }

  Container buildContainer() {
    return Container(
      margin: EdgeInsets.only(bottom: 16),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          buildRow(),
          SizedBox(
            height: 12,
          ),
          buildRow2(),
          buildRow3(),
        ],
      ),
    );
  }

  Row buildRow() {
    Color color1 = Colors.blue;
    switch (widget.entity.listType) {
      case 2:
        //flag = "视频学习";
        color1 = Colors.green;
        break;
      case 3:
        //flag = "培训";
        color1 = Colors.deepOrange;
        break;
      case 4:
        //flag = "考试";
        color1 = Colors.red;
        break;
    }
    return Row(
      children: [
        ClipOval(
          child: Container(
            padding: EdgeInsets.all(12),
            color: color1,
            width: 5,
            height: 5,
          ),
        ),
        Container(
          margin: EdgeInsets.only(left: 6),
          child: Text(
            "${widget.entity.flagType}",
            style: TextStyle(
              fontSize: 12,
              color: color1,
              fontWeight: FontWeight.w500,
            ),
          ),
        )
      ],
    );
  }

  buildRow2() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.only(top: 4),
          child: buildCachedNetworkImage(),
        ),
        SizedBox(
          width: 12,
        ),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                "${widget.entity.title}",
                style: TextStyle(
                  fontSize: 14,
                  color: Color(0xff444444),
                  fontWeight: FontWeight.w400,
                ),
              )
            ],
          ),
        ),
      ],
    );
  }

  Widget buildCachedNetworkImage() {
    if (StringUtils.isEmpty(widget.entity.image)) {
      String path = "assets/images/2.0x/train_icon.png";
      if (widget.entity.listType == 2) {
        path = "assets/images/2.0x/video_icon.png";
      } else if (widget.entity.listType == 3) {
        path = "assets/images/2.0x/train_icon.png";
      }
      return Container(
        width: 100,
        height: 60,
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(4)),
            border: Border.all(
              color: Colors.blueGrey.withOpacity(0.7),
            )),
        child: Image.asset(
          path,
        ),
      );
    }
    return PhysicalModel(
      color: Colors.white,
      shadowColor: Colors.blueGrey,
      borderRadius: BorderRadius.all(Radius.circular(4)),
      elevation: 2.0,
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(4)),
        child: CachedNetworkImage(
          width: 100,
          imageUrl: widget.entity.image,
        ),
      ),
    );
  }

  buildRow3() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Text(
          "${widget.entity.createTime}",
          style: TextStyle(
            fontSize: 12,
            color: Color(0xff666666),
            fontWeight: FontWeight.w400,
          ),
        )
      ],
    );
  }

  /// 1 资讯 ，2 视频， 3 培训 , 4 考试
  String buildHeaderText() {
    String flag = "阅读";
    switch (widget.entity.listType) {
      case 1:
        flag = "阅读";
        break;
      case 2:
        flag = "视频学习";
        break;
      case 3:
        flag = "培训";
        break;
      case 4:
        flag = "考试";
        break;
    }
    return flag;
  }

  ///点击跳转的页面详情
  Widget buildDetailPage() {
    Widget flag;
    switch (widget.entity.listType) {
      case 1:
        flag = NewsDetailePage(
          newsId: widget.entity.id,
          screenSize: MediaQuery.of(context).size,
        );
        break;
      case 2:
        flag = CourseVideoDetailsPage(
          videoCourseId: widget.entity.id2,
          videoId: widget.entity.id,
        );
        break;
      case 3:
        flag = TrainListDetailsPage(
          approveBean: TrainApproveBean(
            widget.entity.id,
            widget.entity.createTime,
            examId: widget.entity.id2,
          ),
        );
        break;
      case 4:
        break;
    }
    return flag;
  }
}
